/*
 * @Author: your name
 * @Date: 2021-06-26 13:10:15
 * @LastEditTime: 2021-06-27 19:50:51
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ttstar\assets\scripts\frameworks\UIWindow.ts
 */

import { SpriteFrame } from "cc";
import { GLoader } from "fairygui-cc";
import FunListener from "../../frameworks/base/FunListener";
import { LayerManager } from "../../frameworks/base/LayerManager";
import { LayerType } from "../../frameworks/base/LayerType";
import { Res } from "../../frameworks/res/Res";
import { UIMediator } from "./UIMediator";
import { UIRoot } from "./UIRoot";
import Utils from "../../frameworks/base/Utils";

export class UIWindow extends UIMediator {

    protected layer: LayerType = LayerType.UI_Main;
    protected isCloseOtherWindow: boolean = false;
    protected isClosing: boolean = false;
    protected statrTick: number = 0;

    // 背景
    protected background!: GLoader | null;
    protected bgUrl!: string | null;

    private _finishListener!: FunListener | null;

    /**
     * 是否能被关闭
     */
    protected _isBeClosed: boolean = true;
    public get isBeClosed() {
        return this._isBeClosed;
    }

    /**
     * 关联表现对象
     */
    protected onAttach() {
        this.statrTick = Date.now();
        super.onAttach();
        if (this.view)
            LayerManager.inst.setLayer(this.view, this.layer);
        if (this.isCloseOtherWindow) {
            this.isCloseOtherWindow = false;
            UIRoot.CloseOtherWindow(this);
        }
    }

    /**
     * 打开窗口，加载界面，这是异步完成的
     * 第一次打开会触发onStart和onEnable
     * 如果窗口已经打开了而且没隐藏，没有然后效果
     * 如果窗口已经打开了但隐藏，会触发onEnable
     * @param parameter 
     */
    public async open(parameter?: any) {
        // this.isClosing = false;
        if (this.isClosing) {
            await Utils.waitDone(() => !this.isClosing, this);
        }

        if (this.view) {
            this.show(parameter);
            if (this.isCloseOtherWindow) {
                this.isCloseOtherWindow = false;
                UIRoot.CloseOtherWindow(this);
            }
            return;
        }
        await this.start(parameter);
        console.log("[Open Window]: " + this.objName);
    }

    // /**
    //  * 打开窗口，加载界面，这是异步完成的
    //  * 第一次打开会触发onStart和onEnable
    //  * 如果窗口已经打开了而且没隐藏，没有然后效果
    //  * 如果窗口已经打开了但隐藏，会触发onEnable
    //  * @param parameter 
    //  */
    // public async openAsync(parameter?: any) {
    //     this.isClosing = false;
    //     if (this.view) {
    //         this.show(parameter);
    //         if (this.isCloseOtherWindow) {
    //             this.isCloseOtherWindow = false;
    //             UIRoot.CloseOtherWindow(this);
    //         }
    //         return;
    //     }
    //     await this.start(parameter);
    // }

    /**
     * 功能和open差不多，只是打开当前窗口会关闭其他窗口
     */
    public async openSingle(parameter?: any) {
        this.isCloseOtherWindow = true;
        await this.open(parameter);
    }

    /**
     * 关闭窗口，会触发onDisable和onDestroy
     * 如果窗口已经关闭了，没有然后效果
     * @param hasAnim 是否有动画 
     */
    public close(hasAnim: boolean = true) {
        if (this.isClosing || this.view == null) return;
        this.isClosing = true;
        this.onClose();
        if (hasAnim) {
            this.onCloseAnimation(() => {
                this.destroy();
                this.isClosing = false;
            });
        }
        else {
            this.destroy();
            this.isClosing = false;
        }

        if (this._finishListener) {
            this._finishListener.runOne();
            this._finishListener = null;
        }
        console.log("[Close Window]: " + this.objName);
    }

    /**
     * 开始动画动画
     * @param finish 
     */
    protected onOpenAnimation(finish: Function) {

    }


    /**
     * 关闭动画
     * @param finish 
     */
    protected onCloseAnimation(finish: Function) {
        finish.call(this);
    }

    /**
     * 关闭
     */
    protected onClose() {

    }

    /**
     * 层级，值越大在越前面
     * @returns 
     */
    public getLayer(): number {
        return this.layer;
    }

    /**
     * 改变层级
     */
    public changeLayer(layer: number) {
        if (this.layer == layer) {
            return;
        }
        this.layer = layer;
        if (this.view)
            LayerManager.inst.setLayer(this.view, layer);
    }

    /**
     * 显示在最前面，注意是当前层级的
     */
    public toFront() {
        if (this.view) {
            let i = this.view.parent._children.length;
            this.view.node.setSiblingIndex(i);
        }
    }

    /**
     * 显示在最后面，注意是当前层级的
     */
    public toBack() {
        if (this.view) {
            this.view.node.setSiblingIndex(0);
        }
    }

    /**
     * 全面显示
     */
    public makeFullScreen() {
        if (this.view)
            this.view.makeFullScreen();
    }

    /**
     * 加载主资源
     */
    protected async loadMain(): Promise<any> {
        await this.loadBackground();
        return await super.loadMain();
    }

    /**
     * 加载背景
     */
    protected async loadBackground() {
        if (this.bgUrl && !this.background) {
            let spriteFrame = await Res.loadSpriteFrameAsync(this.bgUrl);
            if (spriteFrame) {
                this.background = new GLoader();
                this.background.autoSize = true;
                this.background.texture = spriteFrame;
                LayerManager.inst.setLayer(this.background, this.layer);
            }
        }
    }



    /**
     * 回收
     */
    protected clear() {
        if (this.background) {
            Res.recycle(this.background.texture);
            (this.background as any).texture = null;
            this.background.dispose();
            this.background = null;
        }
        super.clear();
    }


    /**
     * 结束回调
     * @param callback 
     * @param target 
     */
    public onFinish(callback: Function, target: any) {
        if (!this._finishListener) {
            this._finishListener = FunListener.create(callback, target);
        }
    }
}